//
//  WHLabel.m
//  VoiceNavigation
//
//  Created by dengweihao on 15/7/3.
//  Copyright (c) 2015年 vcyber. All rights reserved.
//

#import "WHLabel.h"

@implementation WHLabel

+ (WHLabel *)labelWithFrame:(CGRect)frame Text:(NSString *)text Font:(CGFloat)font color:(UIColor *)color {
    WHLabel *myLabel = [[WHLabel alloc] init];
    myLabel.backgroundColor = [UIColor clearColor]; //iOS6,iOS7表现特性不同
    myLabel.text = text;
    myLabel.textColor = color;
    // 设置Label的字体大小
    UIFont *fnt = [UIFont boldSystemFontOfSize:font];
    myLabel.font = fnt;
    // 根据字体得到NSString的尺寸
    CGSize size = [myLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    
    // label的W
    CGFloat LabelW = size.width;
    myLabel.frame = CGRectMake(frame.origin.x, frame.origin.y, LabelW, frame.size.height);
//    myLabel.backgroundColor = [UIColor redColor];
    
    return myLabel;
}

/** 在label中间画一条横线 */
- (void)drawCenterLineInRect:(CGRect)rect {
    //调用super先把文字画上去
    [super drawRect:rect];
    
    //获得上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //设置线条颜色就是标签的文字颜色
    [self.textColor setStroke];
    //画线的起点
    CGFloat y = rect.size.height*0.5;
    CGContextMoveToPoint(context, 0, y);
    
    //根据字体确定线条长度
    CGSize size = [self.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.font, NSFontAttributeName, nil]];
//    CGSize size = [self.text sizeWithFont:self.font];
    
    //线条的终点
    CGContextAddLineToPoint(context, size.width, y);
    //渲染到上下文中
    CGContextStrokePath(context);
}

@end

